﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NMF.Utilities;
using System.Collections;

namespace NMF.Optimizations
{
    public abstract class Optimization
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Optimization()
        {
            this.BaseScenario = CreateBaseScenario();
            this.Patterns = new List<ModificationPattern>();
        }

        public List<ModificationPattern> Patterns { get; private set; }

        public void AddPattern(IObservableSource<IOptimizationModification> pattern)
        {
            Patterns.Add(new DynamicModificationPattern(pattern));
        }

        public void AddPattern(IEnumerable<StaticModification> modifications)
        {
            Patterns.Add(new StaticModificationPattern(modifications));
        }

        public bool ForceModifications { get; protected set; }

        public virtual IEnumerable<IOptimizationModification> GetCurrentModifications()
        {
            return Patterns.SelectMany(p => p.GetCurrentModifications());
        }

        public IOptimizationScenario BaseScenario { get; private set; }

        protected virtual IOptimizationScenario CreateBaseScenario()
        {
            return new BaseScenario(this);
        }

        public void Optimize(IOptimizer optimizer, IScenarioEvaluator evaluator)
        {
            if (optimizer == null) throw new ArgumentNullException("optimizer");
            if (evaluator == null) throw new ArgumentNullException("evaluator");

            optimizer.Optimize(BaseScenario, evaluator);
        }
    }

    public abstract class Optimization<T> : Optimization
    {
        public Optimization(T model)
        {
            if (model == null) throw new ArgumentNullException("model");

            Model = model;
        }

        public T Model { get; private set; }
    }
}
